/* Kanvas Paint and Drawing Tool.
 * Copyright (C) 2011 Darryl Wallace
 * wallacdj@gmail.com / darryl@wallynet.ca
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "PluginManager.h"
#include "PluginInterface/KanvasPluginInterface.h"

PluginManager::PluginManager(QObject *parent)
    : QAbstractItemModel(parent)
{

}

PluginManager::~PluginManager()
{
    foreach(ITool *t, _tools)
    {
        delete t;
    }
    _tools.clear();
    foreach(IFilter *f, _filters)
    {
        delete f;
    }
    _filters.clear();
    foreach(KanvasPluginInterface *k, _plugins)
    {
        delete k;
    }
    _plugins.clear();
}

QVariant PluginManager::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();
    int row = index.row();
    int col = index.column();

    switch (role)
    {
    case Qt::DisplayRole:
        if (col==0)
            return QVariant(_plugins.at(row)->pluginName());
        else if (col==1)
            return QVariant(_plugins.at(row)->authorInfo());
        else if (col==2)
            return QVariant(_plugins.at(row)->authorContact());
    default:
        return QVariant();

    }
}

QModelIndex PluginManager::index(int row, int column, const QModelIndex &parent) const
{
    if ((row>=0 && row < _plugins.size()) && (column>=0 && column<3))
        return createIndex(row,column,0);

    return createIndex(row, column, 0);
}

Qt::ItemFlags PluginManager::flags(const QModelIndex & index) const
{
    if (!index.isValid())
        return 0;
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant PluginManager::headerData(int section, Qt::Orientation orientation,
                    int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
        if (section==0)
            return QVariant("Plugin Name");
        else if (section==1)
            return QVariant("Author Info");
        else if (section==2)
            return QVariant("Author Contact");
        else
            return QVariant();
     return QVariant();
}

QModelIndex PluginManager::parent(const QModelIndex &index) const
{
    return QModelIndex();
}

int PluginManager::rowCount(const QModelIndex &parent) const
{
    return _plugins.size();
}

int PluginManager::columnCount(const QModelIndex &parent) const
{
    return 3;
}

bool PluginManager::addPlugin(QObject *plugin)
{
    KanvasPluginInterface *kplugin  = qobject_cast<KanvasPluginInterface *>(plugin);
    if (!kplugin)
    {
        return false;

    }
    _plugins.append(kplugin);

    foreach (const QMetaObject *mo, kplugin->tools())
    {
        QObject *t = mo->newInstance();
        ITool *tool = qobject_cast<ITool*>(t);
        if (tool)
            _tools.append(tool);

    }

    foreach (const QMetaObject *mo, kplugin->filters())
    {
        QObject *f = mo->newInstance();
        IFilter *filter = qobject_cast<IFilter*>(f);
        if (filter)
            _filters.append(filter);
    }

    return true;

}

QStringList PluginManager::toolNames() const
{
    QStringList l;
    foreach (const ITool *t, _tools)
    {
        l.append(t->toolName());
    }
    return l;
}


